﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace medder.Book.Profiles
{
    public partial class ProfileBook : Form
    {
        MedBaseEntities med = new MedBaseEntities();
        public static ObjectSet<Profile> profiles;
        public ProfileBook()
        {
            profiles = med.Profiles;
            InitializeComponent();
        }

        private void ProfileBook_Load(object sender, EventArgs e)
        {
            bookView.Columns.Add("№", "Идентификатор");
            bookView.Columns.Add("Name", "Название");
            bookView.Columns.Add("TimeNorm", "Время на прием, мин");
            bookView.Columns["TimeNorm"].Width = 120;

            int i = 0;
            foreach (Profile prof in profiles)
            {
                bookView.Rows.Add();

                bookView.Rows[i].Cells[0].Value = prof.ProfId;
                bookView.Rows[i].Cells[1].Value = prof.ProfName;

                bookView.Rows[i].Cells[2].Value = prof.TimeNorm ?? 0;

                i++;
            }

        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (bookView.CurrentRow != null)
            {

                int profId = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                if (profiles.Where(x => x.ProfId == profId).Count() == 1)
                {
                    Profile profile = profiles.Where(x => x.ProfId == profId).FirstOrDefault();
                    profiles.DeleteObject(profile);
                    med.SaveChanges();
                }
                bookView.Rows.Remove(bookView.CurrentRow);
            }
            else
            {
                MessageBox.Show("Выберите строку для удаления");
            }
        }

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bookView.Rows.Add();
        }

        private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bookView.EndEdit();
            if (сохранитьToolStripMenuItem.Enabled)
            {
                foreach (DataGridViewRow xx in bookView.Rows)
                {
                    int profId = Convert.ToInt32(xx.Cells[0].Value);

                    if (profiles.Where(x => x.ProfId == profId).Count() != 1)
                    {
                        Profile profile = profiles.Where(x => x.ProfId == profId).FirstOrDefault();
                        profiles.DeleteObject(profile);
                        med.SaveChanges();
                    }
                }
            }
        }

        private void CheckGrid()
        {

        }


        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ProfileBook_FormClosing(object sender, FormClosingEventArgs e)
        {
            Helper.CloseForm(e, "профилей");
        }

        private void bookView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string text1 = bookView.CurrentCell.Value.ToString();
            string text2 = bookView.CurrentCell.FormattedValue.ToString();
            string text3 = bookView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            bookView.EndEdit();
            string text4 = bookView.CurrentCell.Value.ToString();
            string text5 = bookView.CurrentCell.FormattedValue.ToString();
            string text6 = bookView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            
        }

        private void bookView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string text1 = bookView.CurrentCell.Value.ToString();
            string text2 = bookView.CurrentCell.FormattedValue.ToString();
            string text3 = bookView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
          //  bookView.EndEdit();
            string text4 = bookView.CurrentCell.Value.ToString();
            string text5 = bookView.CurrentCell.FormattedValue.ToString();
            string text6 = bookView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        }



    }
}
